﻿///// Annotations /////

using System;
using System.Collections.Generic;
using System.Collections;
//using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Annotations
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creates a PDF file with annotations.
            // 
            // @param args:  no arguments needed here

            Console.WriteLine("Chapter 15: example Annotations");
            Console.WriteLine("-> Creates a PDF file with annotations;");
            Console.WriteLine("-> resources needed: foxdog.jpg (chapter 5) and foxdog.mpg");
            Console.WriteLine("-> files generated:");
            Console.WriteLine("   annotations.pdf");
            // step 1: creation of a document-object
            Document document = new Document();
            try
            {
                // step 2:
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("annotations.pdf", FileMode.Create));
                // step 3:
                writer.PdfVersion = PdfWriter.VERSION_1_5;
                document.Open();
                // step 4:
                document.Add(new Chunk("top of the page").SetLocalDestination("top"));
                PdfContentByte cb = writer.DirectContent;
                // page 1
                PdfAnnotation annotation = new PdfAnnotation(writer, new Rectangle(100, 750, 150, 800));
                writer.AddAnnotation(annotation);
                annotation.Put(PdfName.SUBTYPE, PdfName.TEXT);
                annotation.Put(PdfName.OPEN, PdfBoolean.PDFTRUE);
                annotation.Put(PdfName.T, new PdfString("custom"));
                annotation.Put(PdfName.CONTENTS, new PdfString("This is a custom built text annotation."));
                cb.Rectangle(100, 750, 50, 50);
                cb.Stroke();

                PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(writer, @"D:\VNThanh\Media\3Com\Nhac\Phi Nhung & Manh Quynh - Dung Noi Xa Nhau.mpg", "Phi Nhung & Manh Quynh - Dung Noi Xa Nhau.mpg", null);
                writer.AddAnnotation(PdfAnnotation.CreateScreen(writer, new Rectangle(200f, 700f, 300f, 800f), "Fox and Dog", fs, "video/mpeg", true));
                PdfAnnotation a = new PdfAnnotation(writer, 200f, 550f, 300f, 650f, PdfAction.JavaScript("app.alert('Hello');\r", writer));
                writer.AddAnnotation(a);
                string str = "some text";
                ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = encoding.GetBytes(str);
                writer.AddAnnotation(PdfAnnotation.CreateFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f), "This is some text", byteArray, null, "some.txt"));
                writer.AddAnnotation(PdfAnnotation.CreateText(writer, new Rectangle(200f, 400f, 300f, 500f), "Help", "This Help annotation was made with 'createText'", false, "Help"));
                writer.AddAnnotation(PdfAnnotation.CreateText(writer, new Rectangle(200f, 250f, 300f, 350f), "Help", "This Comment annotation was made with 'createText'", true, "Comment"));
                cb.Rectangle(200, 700, 100, 100);
                cb.Rectangle(200, 550, 100, 100);
                cb.Rectangle(200, 400, 100, 100);
                cb.Rectangle(200, 250, 100, 100);
                cb.Stroke();
                document.NewPage();
                // page 2
                writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(200f, 700f, 300f, 800f), PdfAnnotation.HIGHLIGHT_INVERT, PdfAction.JavaScript("app.alert('Hello');\r", writer)));
                writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(200f, 550f, 300f, 650f), PdfAnnotation.HIGHLIGHT_OUTLINE, "top"));
                writer.AddAnnotation(PdfAnnotation.CreateLink(writer, new Rectangle(400f, 700f, 500f, 800f), PdfAnnotation.HIGHLIGHT_PUSH, 1, new PdfDestination(PdfDestination.FIT)));
                writer.AddAnnotation(PdfAnnotation.CreatePopup(writer, new Rectangle(400f, 550f, 500f, 650f), "Hello, I'm a popup!", true));

                PdfAnnotation shape1 = PdfAnnotation.CreateSquareCircle(writer, new Rectangle(200f, 400f, 300f, 500f), "This Comment annotation was made with 'createSquareCircle'", false);
                float[] red = { 1, 0, 0 };
                shape1.Put(new PdfName("IC"), new PdfArray(red));
                writer.AddAnnotation(shape1);
                PdfAnnotation shape2 = PdfAnnotation.CreateLine(writer, new Rectangle(200f, 250f, 300f, 350f), "this is a line", 200, 250, 300, 350);
                shape2.Color = BaseColor.BLUE;
                PdfArray lineEndingStyles = new PdfArray();
                lineEndingStyles.Add(new PdfName("Diamond"));
                lineEndingStyles.Add(new PdfName("OpenArrow"));
                shape2.Put(new PdfName("LE"), lineEndingStyles);
                shape2.Put(PdfName.BS, new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
                writer.AddAnnotation(shape2);
                PdfContentByte pcb = new PdfContentByte(writer);
                pcb.SetColorFill(BaseColor.RED);
                PdfAnnotation freeText = PdfAnnotation.CreateFreeText(writer, new Rectangle(400f, 400f, 500f, 500f), "This is some free text, blah blah blah", pcb);
                writer.AddAnnotation(freeText);
                PdfAnnotation attachment = PdfAnnotation.CreateFileAttachment(writer, new Rectangle(400f, 250f, 500f, 350f), "Image of the fox and the dog", null, @"D:\Temp\DD.jpg", "DD.jpg");
                attachment.Put(PdfName.NAME, new PdfString("Paperclip"));
                writer.AddAnnotation(attachment);
            }

            catch (Exception de)
            {
                Console.Error.WriteLine(de.Message);
            }

            // step 5: we close the document
            document.Close();

            Console.ReadLine();
        }
    }
}
